package org.op.editor;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

import javax.swing.Action;

import org.op.editor.doc.DocumentInsertionController;
import org.op.editor.doc.DocumentReloadController;

/**
 * 
 *  This class listens to special keystrokes inside the editor
 * 
 * @author david.peters
 *
 */
public class EditorDirtyKeyAction implements Action {
	
	private EditorComponentView editorView;
	private DocumentReloadController docReloader;
	private DocumentInsertionController docInsertController;

	private int keyType;
	public static final int SPACE_TYPE = 1;
	public static final int ENTER_TYPE = 2;
	
	public EditorDirtyKeyAction() {
		keyType = SPACE_TYPE;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener arg0) {
		// TODO Auto-generated method stub
	}

	public Object getValue(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public void putValue(String arg0, Object arg1) {
		// TODO Auto-generated method stub
	}
	
	public boolean isEnabled() {
		return true;
	}

	public void removePropertyChangeListener(PropertyChangeListener arg0) {
		// TODO Auto-generated method stub
	}

	public void setEnabled(boolean arg0) {
		// TODO Auto-generated method stub
	}

	public void actionPerformed(ActionEvent event) {		
		
		// first, make sure the dirty flag is set for this text component
		editorView.setCurrentDirty();
		
		// will this work on UNIX - LINUX?
		String enterCommand = "\n";
		if (event.getActionCommand().equalsIgnoreCase(enterCommand)) {
			//int caretPos = editorView.getCurrentPane().getCaretPosition();
			docReloader.reloadCurrentLine();
			docInsertController.insertTextIntoCurrentDocument(enterCommand);
			//editorView.getCurrentPane().setCaretPosition(caretPos + 1);
		} else {
			docReloader.reloadCurrentLine();
		}
		
	}

	public EditorComponentView getEditorView() {
		return editorView;
	}

	public void setEditorView(EditorComponentView editorView) {
		this.editorView = editorView;
	}

	public DocumentReloadController getDocReloader() {
		return docReloader;
	}

	public void setDocReloader(DocumentReloadController docReloader) {
		this.docReloader = docReloader;
	}

	public int getKeyType() {
		return keyType;
	}

	public void setKeyType(int keyType) {
		this.keyType = keyType;
	}

	public DocumentInsertionController getDocInsertController() {
		return docInsertController;
	}

	public void setDocInsertController(
			DocumentInsertionController docInsertController) {
		this.docInsertController = docInsertController;
	}

}
